package com.baijiayun.live.ui.speakpanel

import android.app.Activity
import android.app.Dialog
import androidx.annotation.StringRes
import android.view.LayoutInflater
import android.view.ViewGroup
import com.baijia.bjydialog.MaterialDialog
import com.baijiayun.live.ui.R
import com.baijiayun.live.ui.activity.LiveRoomBaseActivity
import com.baijiayun.live.ui.activity.LiveRoomRouterListener
import com.baijiayun.live.ui.speakerlist.item.*
import com.baijiayun.livecore.context.LiveRoom
import com.baijiayun.livecore.models.LPUserModel
import com.baijiayun.liveuibase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.widgets.dialog.ThemeMaterialDialogBuilder
import java.util.*

/**
 * Created by pengwenjian on 2020/10/19
 * Describe:
 */
class PlaceholderItem(rootView: ViewGroup, routerListener: LiveRoomRouterListener) : BaseSwitchItem(routerListener), Playable {
    val context: Activity by lazy {
        rootView.context as Activity
    }
    val liveRoom: LiveRoom by lazy {
        routerListener.liveRoom
    }
    var container: ViewGroup = LayoutInflater.from(context).inflate(R.layout.bjy_layout_item_video, rootView, false) as ViewGroup
    private var optionsDialog: Dialog? = null
    private var switchDialog: Dialog? = null

    init {
        registerClickEvent(container)
        status = SwitchableStatus.MainVideo
    }

    private fun getString(@StringRes resId: Int): String {
        return context.getString(resId)
    }

    override fun getSwitchableType() = SwitchableType.MainItem
    override fun enableClearScreen(): Boolean {
        return true
    }

    override fun showOptionDialog() {
        val options: MutableList<String?> = ArrayList()
        if (isInFullScreen) {
            options.add(getString(R.string.live_full_screen_exit))
        } else {
            //坐席区为了兼容iOS，暂时不加全屏选项
            if (switchableType != SwitchableType.SpeakItem) {
                options.add(getString(R.string.live_full_screen))
            }
        }
        //全屏时没有切换窗口选项
        if (!isInFullScreen && canSwitchPPTAndMainVideo()) {
            options.add(getString(R.string.live_max_screen))
        }
        if (context.isFinishing) return
        optionsDialog = ThemeMaterialDialogBuilder(context)
                .items(options)
                .itemsCallback { materialDialog: MaterialDialog,_, _, charSequence: CharSequence ->
                    if (context.isFinishing || context.isDestroyed) return@itemsCallback
                    when {
                        getString(R.string.live_max_screen) == charSequence.toString() -> {
                            switchPPTVideoSync()
                        }
                        getString(R.string.live_full_screen) == charSequence.toString() -> {
                            switchToFullScreen(true)
                        }
                        getString(R.string.live_full_screen_exit) == charSequence.toString() -> {
                            switchToFullScreen(false)
                        }
                    }
                    materialDialog.dismiss()
                }
                .show()
    }

    override fun isPlaceholderItem() = true
    override fun switchPPTVideoSync() {
        if (liveRoom.isSyncPPTVideo
            && (liveRoom.isTeacherOrAssistant || liveRoom.isGroupTeacherOrAssistant)
            && liveRoom.isClassStarted) {
            showSwitchDialog()
        } else {
            switch2MaxScreenLocal()
        }
    }

    private fun showSwitchDialog() {
        if (context !is LiveRoomBaseActivity) {
            return
        }
        if (context.isFinishing || context.isDestroyed) {
            return
        }
        context.let {
            switchDialog = ThemeMaterialDialogBuilder(it)
                    .title(getString(R.string.live_exit_hint_title))
                    .content(getString(R.string.live_pad_sync_video_ppt))
                    .positiveText(R.string.live_pad_switch_sync)
                    .negativeText(R.string.live_pad_switch_local)
                    .negativeColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(it, R.attr.base_theme_dialog_negative_text_color))
                    .onPositive { _, _ -> switch2MaxScreenSync() }
                    .onNegative { _, _ -> switch2MaxScreenLocal() }
                    .show()
        }
    }

    private fun switch2MaxScreenLocal() {
        super.switchPPTVideoSync()
    }

    private fun switch2MaxScreenSync() {
        liveRoom.requestPPTVideoSwitch(true)
        switch2MaxScreenLocal()
    }

    override fun getItemType() = SpeakItemType.Presenter

    override fun getView() = container

    override fun hasAudio() = false

    override fun isAudioStreaming() = false

    override fun isStreaming() = false

    override fun refreshPlayable() {
        // do nothing
    }

    override fun refreshPlayable(videoOn: Boolean, audioOn: Boolean) {
        // do nothing
    }

    override fun notifyAwardChange(count: Int) {
        // do nothing
    }

    override fun stopStreaming() {
        // do nothing
    }

    override fun getUser() = LPUserModel()

    override fun hasVideo() = false

    override fun isVideoStreaming() = false

    override fun getIdentity() = ""

    fun onRemove() {
        if (optionsDialog?.isShowing == true) {
            optionsDialog?.dismiss()
        }
        if (switchDialog?.isShowing == true) {
            switchDialog?.dismiss()
        }
    }
}